﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using YArchitech.Controls;
using YJKOpeningCommon.DataEnum;
using YJKOpeningCommon.DbOpening;
using YJKOpeningCommon.OpeningProfile;

namespace YJKOpeningCommon.Public
{
	public class DbOpeningImpExpManager
	{
		private DbOpeningBase CreateDbOpeningBaseByReflect(XmlNode xmlNode)
		{
			string typeName = "YJKOpeningCommon.DbOpening." + Convert.ToString(xmlNode.Attributes["OpeningClassName"].Value);
			DbOpeningBase dbOpeningBase = this.m_assembly.CreateInstance(typeName) as DbOpeningBase;
			if (dbOpeningBase == null)
			{
				return dbOpeningBase;
			}
			if (!dbOpeningBase.ImportByXmlNode(xmlNode))
			{
				return null;
			}
			return dbOpeningBase;
		}

		private XmlNode CreateXmlDocument()
		{
			this.m_xmlDoc = new XmlDocument();
			XmlDeclaration newChild = this.m_xmlDoc.CreateXmlDeclaration("1.0", "gb2312", null);
			return this.m_xmlDoc.AppendChild(newChild);
		}

		private OpeningProfileBase CreatingOpeningProfileBaseByReflect(XmlNode xmlNodeRoot)
		{
			string typeName = "YJKOpeningCommon.OpeningProfile." + Convert.ToString(xmlNodeRoot.Attributes["OpeningProfileName"].Value);
			OpeningProfileBase openingProfileBase = this.m_assembly.CreateInstance(typeName) as OpeningProfileBase;
			if (openingProfileBase == null)
			{
				return openingProfileBase;
			}
			if (!openingProfileBase.ImportByXmlNode(xmlNodeRoot))
			{
				return null;
			}
			return openingProfileBase;
		}

		private bool IsCompositeOpeningXmlNode(XmlNode xmlNode)
		{
			return xmlNode.ChildNodes.Count != 0;
		}

		private bool IsValidFileVersion(out XmlNode root)
		{
			if (!this.GetXmlNodeValidItemRoot(out root))
			{
				YJKMessageBox.Show("得到XML中节点DataItems时出错！", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				return false;
			}
			string text = string.Empty;
			try
			{
				text = Convert.ToString(root.Attributes["Version"].Value);
				this.m_strDesignVersion = text;
				this.m_strDesignUser = Convert.ToString(root.Attributes["DesignUser"].Value);
				this.m_strDesignDate = Convert.ToString(root.Attributes["DesignDate"].Value);
				this.m_strSelectModel = Convert.ToString(root.Attributes["SelectModel"].Value);
			}
			catch (Exception)
			{
				YJKMessageBox.Show("读取XML根节点DataItems属性：Version、DesignUser或DesignDate时出错！", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				return false;
			}
			if (text.CompareTo(DbOpeningImpExpManager.m_strVersion) == 0)
			{
				return true;
			}
			YJKMessageBox.Show("xml版本不一致！", MessageBoxButtons.OK, MessageBoxIcon.Hand);
			return false;
		}

		private bool LoadXmlDocument(ref XmlReader reader)
		{
			bool result;
			try
			{
				if (!File.Exists(this.m_strXmlPathFileName))
				{
					YJKMessageBox.Show("无效的路径", MessageBoxButtons.OK, MessageBoxIcon.Hand);
					result = false;
				}
				else
				{
					this.m_xmlDoc = new XmlDocument();
					XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
					xmlReaderSettings.IgnoreComments = true;
					reader = XmlReader.Create(this.m_strXmlPathFileName, xmlReaderSettings);
					this.m_xmlDoc.Load(reader);
					result = true;
				}
			}
			catch (Exception)
			{
				YJKMessageBox.Show("加载XML文档失败：\n", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				result = false;
			}
			return result;
		}

		private bool SaveXmlDocument()
		{
			bool result;
			try
			{
				this.m_xmlDoc.Save(this.m_strXmlPathFileName);
				result = true;
			}
			catch
			{
				YJKMessageBox.Show("保持XmlDocument文档失败：\n", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				result = false;
			}
			return result;
		}

		private bool CreateXmlNodeUserAndDate(XmlNode xmlNodeRoot, ESelectModel eSelectModel)
		{
			try
			{
				XmlAttribute xmlAttribute = this.m_xmlDoc.CreateAttribute("Version");
				if (this.m_bIsOpenOperator)
				{
					xmlAttribute.Value = "2.0";
				}
				else
				{
					xmlAttribute.Value = "2.0";
				}
				xmlNodeRoot.Attributes.Append(xmlAttribute);
				if (eSelectModel != ESelectModel.eNone)
				{
					XmlAttribute xmlAttribute2 = this.m_xmlDoc.CreateAttribute("SelectModel");
					switch (eSelectModel)
					{
					case ESelectModel.ePickBox:
						xmlAttribute2.Value = "PickBox";
						break;
					case ESelectModel.eLevel:
						xmlAttribute2.Value = "Level";
						break;
					case ESelectModel.eProject:
						xmlAttribute2.Value = "Project";
						break;
					}
					xmlNodeRoot.Attributes.Append(xmlAttribute2);
				}
				XmlAttribute xmlAttribute3 = this.m_xmlDoc.CreateAttribute("DesignUser");
				xmlAttribute3.Value = this.m_strDesignUser;
				xmlNodeRoot.Attributes.Append(xmlAttribute3);
				XmlAttribute xmlAttribute4 = this.m_xmlDoc.CreateAttribute("DesignDate");
				if (this.m_bIsOpenOperator)
				{
					xmlAttribute4.Value = this.m_strDesignDate;
				}
				else
				{
					this.m_strDesignDate = DateTime.Now.ToString();
					xmlAttribute4.Value = this.m_strDesignDate;
				}
				xmlNodeRoot.Attributes.Append(xmlAttribute4);
			}
			catch (Exception)
			{
				YJKMessageBox.Show("创建XML版本号、提资用户名称或日期节点时出错：\n", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				return false;
			}
			return true;
		}

		private bool GetXmlNodeValidItemRoot(out XmlNode root)
		{
			bool result;
			try
			{
				root = this.m_xmlDoc.SelectSingleNode("DataItems");
				result = true;
			}
			catch (Exception)
			{
				root = null;
				result = false;
			}
			return result;
		}

		public DbOpeningImpExpManager(string strFilePathName)
		{
			this.m_strXmlPathFileName = strFilePathName;
		}

		public void SetOpenOperatorState(bool bIsOpenOperator)
		{
			this.m_bIsOpenOperator = bIsOpenOperator;
		}

		public void SetDesignUser(string strDesignUser)
		{
			this.m_strDesignUser = strDesignUser;
		}

		public bool ExportOpening2Xml(LinkedList<DbOpeningBase> lstOpenings, ESelectModel eSelectModel)
		{
			try
			{
				this.CreateXmlDocument();
				XmlNode xmlNode = this.m_xmlDoc.CreateElement("DataItems");
				this.m_xmlDoc.AppendChild(xmlNode);
				if (!this.CreateXmlNodeUserAndDate(xmlNode, eSelectModel))
				{
					YJKMessageBox.Show("创建用户名和日期时出错！", MessageBoxButtons.OK, MessageBoxIcon.Hand);
					return false;
				}
				foreach (DbOpeningBase dbOpeningBase in lstOpenings)
				{
					XmlElement xmlElement = this.m_xmlDoc.CreateElement("DataItem");
					if (dbOpeningBase.Export2XmlNode(xmlElement))
					{
						xmlNode.AppendChild(xmlElement);
					}
				}
				if (!this.SaveXmlDocument())
				{
					return false;
				}
			}
			catch (Exception)
			{
				YJKMessageBox.Show("创建xml文档时出错！", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				return false;
			}
			return true;
		}

		public bool ImportOpeningByXml(out LinkedList<DbOpeningBase> openingList)
		{
			openingList = new LinkedList<DbOpeningBase>();
			XmlReader xmlReader = null;
			if (!this.LoadXmlDocument(ref xmlReader))
			{
				return false;
			}
			try
			{
				XmlNode xmlNode = null;
				if (!this.IsValidFileVersion(out xmlNode))
				{
					xmlReader.Close();
					return false;
				}
				this.m_assembly = Assembly.GetExecutingAssembly();
				foreach (object obj in xmlNode.ChildNodes)
				{
					XmlNode xmlNode2 = (XmlNode)obj;
					DbOpeningBase dbOpeningBase = this.CreateDbOpeningBaseByReflect(xmlNode2);
					OpeningProfileBase openingProfileBase = this.CreatingOpeningProfileBaseByReflect(xmlNode2);
					if (dbOpeningBase != null && openingProfileBase != null)
					{
						dbOpeningBase.m_profile = openingProfileBase;
						dbOpeningBase.SetDesignDate(this.m_strDesignDate);
						dbOpeningBase.SetDesignUser(this.m_strDesignUser);
						dbOpeningBase.SetSelectModel(this.m_strSelectModel);
						if (this.IsCompositeOpeningXmlNode(xmlNode2))
						{
							DbCompositeOpening dbCompositeOpening = dbOpeningBase as DbCompositeOpening;
							if (dbCompositeOpening == null)
							{
								continue;
							}
							dbCompositeOpening.m_lstDbFamilyOpening = new List<DbOpeningBase>();
							foreach (object obj2 in xmlNode2.ChildNodes)
							{
								XmlNode xmlNode3 = (XmlNode)obj2;
								OpeningProfileBase openingProfileBase2 = this.CreatingOpeningProfileBaseByReflect(xmlNode3);
								DbOpeningBase dbOpeningBase2 = this.CreateDbOpeningBaseByReflect(xmlNode3);
								if (dbOpeningBase2 != null && openingProfileBase2 != null)
								{
									dbOpeningBase2.m_profile = openingProfileBase2;
									dbCompositeOpening.m_lstDbFamilyOpening.Add(dbOpeningBase2);
								}
							}
						}
						openingList.AddLast(dbOpeningBase);
					}
				}
			}
			catch (Exception)
			{
			}
			xmlReader.Close();
			return true;
		}

		private static string m_strVersion = "2.0";

		private string m_strXmlPathFileName;

		private XmlDocument m_xmlDoc;

		private Assembly m_assembly;

		private string m_strDesignUser;

		private string m_strDesignDate;

		private string m_strDesignVersion;

		private string m_strSelectModel;

		private bool m_bIsOpenOperator;
	}
}
